package org.dean.webapps.projectname.utils;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: dean
 * Date: 9/16/11
 * Time: 11:53 AM
 * To change this template use File | Settings | File Templates.
 */
public class NumberUtils {

    public static Double formatDouble(Double value, int scale) {
        if (value != null) {
            double scaleVal = Math.pow(10, (double) scale);
            Double formatValue = ((Double) (value * scaleVal)).intValue() / scaleVal;
            return formatValue;
        } else {
            return value;
        }
    }

    public static String formatDigital(Locale locale, BigDecimal digital) {
        try {
            NumberFormat nf = NumberFormat.getInstance(locale);
            nf.setMaximumFractionDigits(2);
            nf.setMinimumFractionDigits(2);
            String digitalStr = nf.format(digital);
            return digitalStr;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatDigital(Double doubleVal) {
        try {
            Locale locale = Locale.getDefault();
            BigDecimal digital = new BigDecimal(doubleVal);
            NumberFormat nf = NumberFormat.getInstance(locale);
            nf.setMaximumFractionDigits(2);
            nf.setMinimumFractionDigits(2);
            String digitalStr = nf.format(digital);
            return digitalStr;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatDigital(BigDecimal digital) {
        return formatDigital(Locale.getDefault(),digital);
    }
    public static String formatDigital(double digital) {
        return formatDigital(new BigDecimal(digital));
    }

}
